package com.h3c.nem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.h3c.nem.client.IttpClient;
import com.h3c.nem.dto.LoginUserDTO;
import com.h3c.nem.dto.TbApprovalRecordDTO;
import com.h3c.nem.dto.TbFlowInstDTO;
import com.h3c.nem.dto.TbWorkerDTO;
import com.h3c.nem.dto.client.IttpClientDTO;
import com.h3c.nem.dto.form.ConfigTheFormSubmit;
import com.h3c.nem.entity.*;
import com.h3c.nem.mapper.*;
import com.h3c.nem.mapstruct.TbApprovalRecordMapstruct;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ITbApprovalRecordService;
import com.h3c.nem.utils.AppBusinessException;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.utils.CommonUtil;
import com.h3c.nem.vo.TbApprovalRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: TbApprovalRecordServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-01-19
 * Description: tb_approval_record serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class TbApprovalRecordServiceImpl extends ServiceImpl<TbApprovalRecordMapper, TbApprovalRecord> implements ITbApprovalRecordService {

    @Resource
    private TbApprovalRecordMapper tbApprovalRecordMapper;
    @Autowired
    CommService commService;
    @Resource
    TbFlowInstMapper tbFlowInstMapper;
    @Autowired
    TbFlowInstServiceImpl tbFlowInstService;
    @Resource
    SdUploadFileMapper sdUploadFileMapper;
    @Resource
    TbWorkerMapper tbWorkerMapper;
    @Resource
    TbWorkerGradeMapper tbWorkerGradeMapper;
    @Resource
    IttpClient ittpClient;


    @Override
    public void saveRecord(String flowId, Integer approvalType, String approvalUserId, String approvalUserInfo, String approvalNote) {
        saveRecord(flowId, approvalType, approvalUserId, approvalUserInfo, approvalNote, null);
    }

    @Override
    public void saveRecord(String flowId, Integer approvalType, String approvalUserId, String approvalUserInfo,
                           String approvalNote, Integer approvalResult) {
        saveRecord(flowId, approvalType, approvalUserId, approvalUserInfo, approvalNote, approvalResult, "", new Date(), new Date());
    }

    @Override
    public void saveRecord(String flowId, Integer approvalType, String approvalUserId, String approvalUserInfo,
                           String approvalNote, Integer approvalResult, String score, Date beginDate, Date endDate) {
        TbApprovalRecord approvalRecord = new TbApprovalRecord();
        approvalRecord.setApprovalId(IdWorker.nextId() + "");
        approvalRecord.setFlowId(StringUtils.hasText(flowId) ? flowId : approvalUserId);
        approvalRecord.setFlowArriveTime(beginDate);
        approvalRecord.setApprovalType(approvalType);
        approvalRecord.setApprovalTime(endDate);
        approvalRecord.setApprovalUserId(approvalUserId);
        approvalRecord.setApprovalUserInfo(approvalUserInfo);
        approvalRecord.setApprovalNote(approvalNote);
        if (approvalResult != null) {
            approvalRecord.setApprovalResult(approvalResult);
        }
        approvalRecord.setApprovalScore(score);
        save(approvalRecord);
    }

    /**
     * @Title: queryApprovalDraft
     * @Description: 月度考核/转正考核评价 草稿内容
     * @Params: [com.h3c.nem.dto.TbApprovalRecordDTO]
     * @Return: com.h3c.nem.entity.TbApprovalRecord
     * @Author: f23104
     * @Time: 2022/2/8 14:28
     */
    @Override
    public TbApprovalRecord queryApprovalDraft(TbApprovalRecordDTO dto) {
        LoginUserDTO loginUserDTO = commService.getLoginUserDTO();
        String userId = loginUserDTO.getUserId();
        String flowId = dto.getFlowId();
        if (StringUtils.isEmpty(flowId)) {
            throw new AppBusinessException("未关联流程实例");
        }
        Integer approvalType = dto.getApprovalType();
        if (StringUtils.isEmpty(approvalType)) {
            throw new AppBusinessException("未传入审批类型");
        }

        TbApprovalRecord query = new TbApprovalRecord();
        query.setFlowId(flowId);
        query.setApprovalType(approvalType);
        query.setApprovalUserId(userId);
        return tbApprovalRecordMapper.queryExistApproval(query);
    }

    /**
     * @Title: queryApprovalRecord
     * @Description: 审批记录 及 相关附件
     * @Params: [com.h3c.nem.dto.TbApprovalRecordDTO]
     * @Return: java.util.List<com.h3c.nem.entity.TbApprovalRecord>
     * @Author: f23104
     * @Time: 2022/2/8 14:27
     */
    @Override
    public Map<String, Object> queryApprovalRecord(TbApprovalRecordDTO dto) {
        String workerId = dto.getWorkerId();
        String loginUserId = commService.getLoginUserId();
        List<TbFlowInst> tbFlowInsts = tbFlowInstMapper.queryFlowInstInOrder(workerId);
        //按顺序放好的填写流程
        Map<Integer, TbFlowInst> fillInstMap = new HashMap<>();
        //按顺序放好的评价流程
        Map<Integer, TbFlowInst> examInstMap = new HashMap<>();
        int fillCount = 1;
        int examCount = 1;
        for (TbFlowInst inst : tbFlowInsts) {
            String flowCode = inst.getFlowCode();
            if (flowCode.equals(BusiEnumDefine.FLOW_MONTH_FILL) || flowCode.equals(BusiEnumDefine.FLOW_TRANSFER_FILL)) {
                if (fillInstMap.get(1) == null) {
                    fillInstMap.put(1, inst);
                } else {
                    fillInstMap.put(++fillCount, inst);
                }
            }
            if (flowCode.equals(BusiEnumDefine.FLOW_MONTH_APPRAISAL) || flowCode.equals(BusiEnumDefine.FLOW_TRANSFER_APPRAISAL)) {
                if (examInstMap.get(1) == null) {
                    examInstMap.put(1, inst);
                } else {
                    examInstMap.put(++examCount, inst);
                }
            }
        }
        Map<String, String> flowIdMap = new HashMap<>();
        for (int i = 0; i < fillCount; i++) {
            TbFlowInst fillInst = fillInstMap.get(i + 1);
            TbFlowInst examInst = examInstMap.get(i + 1);
            if (fillInst != null && examInst != null) {
                flowIdMap.put(fillInst.getFlowId(), examInst.getFlowId());
                flowIdMap.put(examInst.getFlowId(), fillInst.getFlowId());
            }
        }

        String flowId = dto.getFlowId();
        QueryWrapper<TbApprovalRecord> approvalWrapper = Wrappers.query();
//        approvalWrapper.in("approval_type", BusiEnumDefine.FLOW_STATUS_MONTH_FILL_PLAN,
//                        BusiEnumDefine.FLOW_STATUS_MONTH_CHECK_PLAN,
//                        BusiEnumDefine.FLOW_STATUS_MONTH_EXAM,
//                        BusiEnumDefine.FLOW_STATUS_FINAL_FILL_PLAN,
//                        BusiEnumDefine.FLOW_STATUS_FINAL_CHECK_PLAN,
//                        BusiEnumDefine.FLOW_STATUS_FINAL_EXAM)

        approvalWrapper.isNotNull("approval_time")
                .orderByDesc("approval_time");

        QueryWrapper<SdUploadFile> fileWrapper = Wrappers.query();
        fileWrapper.eq("busi_type", BusiEnumDefine.PROMISE_ATTACHMENT);

        if (flowIdMap.get(flowId) != null) {
            approvalWrapper.in("flow_id", flowIdMap.get(flowId), flowId);
            fileWrapper.in("busi_id", flowIdMap.get(flowId), flowId);
        } else {
            approvalWrapper.in("flow_id", flowId);
            fileWrapper.in("busi_id", flowId);
        }

        List<TbApprovalRecord> tbApprovalRecords = tbApprovalRecordMapper.selectList(approvalWrapper);
        List<TbApprovalRecordDTO> recordDTOS = new ArrayList<>();
        tbApprovalRecords.forEach(record -> {
            TbApprovalRecordDTO recordDTO = JSONObject.parseObject(JSONObject.toJSONString(record), TbApprovalRecordDTO.class);
            if (recordDTO.getApprovalType() == BusiEnumDefine.flowStatusMonthlyPromiseCheck && !loginUserId.equals(workerId)) {
                String approvalScore = recordDTO.getApprovalScore();
                String approvalNote = recordDTO.getApprovalNote();
                recordDTO.setApprovalNote("考核评价：" + approvalScore + " 评价说明：" + approvalNote);
            }
            recordDTOS.add(recordDTO);
        });
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("approvalRecord", recordDTOS);

        List<SdUploadFile> sdUploadFiles = sdUploadFileMapper.selectList(fileWrapper);
        returnMap.put("fileList", sdUploadFiles);

        return returnMap;
    }

    /**
     * @Title: approve
     * @Description: 审批  传入flowId，submit，approvalType
     * @Params: [com.h3c.nem.dto.TbApprovalRecordDTO]
     * @Return: java.lang.Integer
     * @Author: f23104
     * @Time: 2022/2/8 9:49
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer approve(TbApprovalRecordDTO dto) {
        SdAllWorker loginUserDTO = commService.getWorkerIdByDomain(dto.getWorkerDomain());
        String userId = loginUserDTO.getWorkerId();
        String userInfo = loginUserDTO.getWorkerName() + "/" + userId;
        String flowId = dto.getFlowId();
        if (StringUtils.isEmpty(flowId)) {
            throw new AppBusinessException("未关联流程实例");
        }
        TbFlowInst tbFlowInst = tbFlowInstMapper.selectById(flowId);
        TbFlowInstDTO tbFlowInstDTO = JSONObject.parseObject(JSONObject.toJSONString(tbFlowInst), TbFlowInstDTO.class);
        TbWorker tbWorker = tbWorkerMapper.selectById(tbFlowInst.getWorkerId());
        TbWorkerDTO tbWorkerDTO = JSONObject.parseObject(JSONObject.toJSONString(tbWorker), TbWorkerDTO.class);
        tbWorkerDTO.setFlowId(flowId);
        Date flowArriveTime = tbFlowInst.getArriveTime();
        boolean submit = dto.getSubmit();
        Integer approvalType = dto.getApprovalType();
        Integer approvalResult = dto.getApprovalResult();
        if (StringUtils.isEmpty(approvalType)) {
            throw new AppBusinessException("未传入审批类型");
        }
        //审批记录
        TbApprovalRecord query = new TbApprovalRecord();
        query.setFlowId(flowId);
        query.setApprovalType(approvalType);
        query.setApprovalUserId(userId);
        TbApprovalRecord existApproval = tbApprovalRecordMapper.queryExistApproval(query); //是否有存在的草稿记录 approvalTime为空

        if (existApproval != null) {
            existApproval.setApprovalResult(dto.getApprovalResult());//审批结果
            existApproval.setApprovalScore(dto.getApprovalScore());//审批评分
            existApproval.setApprovalNote(dto.getApprovalNote());//审批说明
            existApproval.setApproveChannel(dto.getApproveChannel());
            if (submit) {
                existApproval.setApprovalTime(new Date());
            }
            tbApprovalRecordMapper.updateTbApprovalRecord(existApproval);
        } else {
            TbApprovalRecord approvalRecord = new TbApprovalRecord();
            approvalRecord.setApprovalId(IdWorker.nextId() + "");
            approvalRecord.setFlowId(flowId);
            approvalRecord.setFlowArriveTime(flowArriveTime);
            approvalRecord.setApprovalType(approvalType);
            approvalRecord.setApprovalUserId(userId);
            approvalRecord.setApprovalUserInfo(userInfo);
            approvalRecord.setApprovalResult(dto.getApprovalResult());//审批结果
            approvalRecord.setApprovalScore(dto.getApprovalScore());//审批评分
            approvalRecord.setApprovalNote(dto.getApprovalNote());//审批说明
            approvalRecord.setApproveChannel(dto.getApproveChannel());
            if (submit) {   //保存草稿不set审批时间
                approvalRecord.setApprovalTime(new Date());
            }
            tbApprovalRecordMapper.insertTbApprovalRecord(approvalRecord);
        }
        //流程流转
        if (submit) {
            //承诺审核
            if (approvalType.equals(BusiEnumDefine.flowStatusMonthlyPromiseExamine)
                    || approvalType.equals(BusiEnumDefine.flowStatusTurnFormalExamine)) {
                if (StringUtils.isEmpty(approvalResult)) {
                    throw new AppBusinessException("未传入审核结果");
                }
                if (approvalResult.equals(BusiEnumDefine.APPROVAL_APPROVE)) {
                    tbFlowInstService.forwardSpecificFlowInst(tbWorkerDTO, tbFlowInstDTO);
                } else if (approvalResult.equals(BusiEnumDefine.APPROVAL_BACKUP)) {
                    tbFlowInstService.backSpecificFlowInst(tbWorkerDTO);
                }
                //月度/转正考核
            } else if (approvalType.equals(BusiEnumDefine.flowStatusMonthlyPromiseCheck)
                    || approvalType.equals(BusiEnumDefine.flowStatusTurnFormalCheck)) {
                int flowInt = tbFlowInstService.forwardSpecificFlowInst(tbWorkerDTO, tbFlowInstDTO);
                if (flowInt == BusiEnumDefine.flowStatusFinished) {
                    if (approvalType.equals(BusiEnumDefine.flowStatusMonthlyPromiseCheck)) {
                        tbWorkerDTO.setFlowResult(dto.getApprovalScore());
                    } else if (approvalType.equals(BusiEnumDefine.flowStatusTurnFormalCheck)) {
                        //应届生转正考核时需要打分
                        if("应届毕业生".equals(tbWorkerDTO.getDataSrc())){
                            String approvalRes = dto.getApprovalResult() == BusiEnumDefine.APPROVAL_APPROVE ? "通过" : "不通过";
                            tbWorkerDTO.setFlowResult(dto.getApprovalScore()+":"+approvalRes);
                            log.info("1111111111111111111"+JSONObject.toJSONString(tbWorkerDTO));
                        }else{
                            tbWorkerDTO.setFlowResult(dto.getApprovalResult() == BusiEnumDefine.APPROVAL_APPROVE ? "通过" : "不通过");

                            log.info("222222222222222222"+JSONObject.toJSONString(tbWorkerDTO));
                        }

                    }
                    tbWorkerDTO.setFlowId(flowId);
                    addWorkerGrade(tbWorkerDTO, userInfo);
                }
                if (approvalType.equals(BusiEnumDefine.flowStatusTurnFormalCheck)) {
                    //最后一个审批人 审批完worker更新状态
                    String finalJudger = tbWorkerDTO.getFinalJudger();
                    String approvedUserIds = tbFlowInstDTO.getApprovedUserIds();
                    if (!StringUtils.isEmpty(finalJudger) && !StringUtils.isEmpty(approvedUserIds)) {
                        //当前登录的为最后一个审批人
                        if (finalJudger.contains(userId)) {
                            if (!StringUtils.isEmpty(dto.getApprovalResult())) {
                                if (dto.getApprovalResult().equals(BusiEnumDefine.APPROVAL_APPROVE)) {
                                    tbWorker.setWorkerStatus(BusiEnumDefine.WORKER_STATUS_CORRECTED);
                                } else {
                                    tbWorker.setWorkerStatus(BusiEnumDefine.WORKER_STATUS_ELIMINATED);
                                }
                                tbWorkerMapper.updateById(tbWorker);
                            }
                            //当前不是最后一个审批人。但approvedUser中有过他，则当前登录的为转办的人
                        } else if (approvedUserIds.contains(finalJudger.split("/")[1])) {
                            if (!StringUtils.isEmpty(dto.getApprovalResult())) {
                                if (dto.getApprovalResult().equals(BusiEnumDefine.APPROVAL_APPROVE)) {
                                    tbWorker.setWorkerStatus(BusiEnumDefine.WORKER_STATUS_CORRECTED);
                                } else {
                                    tbWorker.setWorkerStatus(BusiEnumDefine.WORKER_STATUS_ELIMINATED);
                                }
                                tbWorkerMapper.updateById(tbWorker);
                            }
                        }
                    }
                }
            }
        }
        return 1;
    }

    //最终成绩插到worker_grade表
    public void addWorkerGrade(TbWorkerDTO workerDTO, String evaluatorInfo) {
        String flowId = workerDTO.getFlowId();
        String workerId = workerDTO.getWorkerId();
        QueryWrapper<TbFlowInst> flowWrapper = Wrappers.query();
        flowWrapper.eq("worker_id", workerId)
                .in("flow_code", BusiEnumDefine.flowCodeMonthlyCheck, BusiEnumDefine.flowCodeTurnFormalCheck)
                .orderByAsc("flow_id");
        List<TbFlowInst> tbFlowInsts = tbFlowInstMapper.selectList(flowWrapper);

        Date joinDate = workerDTO.getJoinDate();
        Calendar instance = Calendar.getInstance();
        instance.setTime(joinDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        int i = 1;
        for (TbFlowInst inst : tbFlowInsts) {
            String instFlowId = inst.getFlowId();
            if (instFlowId.equals(flowId)) {
                instance.add(Calendar.MONTH, i);
                Date finishMonth = instance.getTime();
                String scoreMonth = sdf.format(finishMonth);
                TbWorkerGrade grade = new TbWorkerGrade();
                grade.setWorkerId(workerId);
                grade.setWorkerName(workerDTO.getWorkerName());
                grade.setFlowId(flowId);
                grade.setScoreMonth(scoreMonth);
                grade.setWorkerGrade(workerDTO.getFlowResult());
                grade.setEvaluateTime(new Date());
                grade.setEvaluatorId(evaluatorInfo);
                log.info("333333333333333"+JSONObject.toJSONString(grade));
                tbWorkerGradeMapper.insertTbWorkerGrade(grade);
                break;
            } else {
                i++;
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void wxApprove(ConfigTheFormSubmit formSubmit) {
        JSONObject data = JSONObject.parseObject(StringUtils.isEmpty(formSubmit.getData()) ? "" : formSubmit.getData());
        String docId = formSubmit.getDocId();
        TbFlowInst tbFlowInst = tbFlowInstMapper.selectById(docId);
        TbFlowInstDTO tbFlowInstDTO = JSONObject.parseObject(JSONObject.toJSONString(tbFlowInst), TbFlowInstDTO.class);
        String flowCode = tbFlowInst.getFlowCode();
        Integer approvalType = null;
        String defaultApproveNote = "";
        if (BusiEnumDefine.FLOW_MONTH_FILL.equals(flowCode)) {
            //月度承诺填写流程
            approvalType = BusiEnumDefine.FLOW_STATUS_MONTH_CHECK_PLAN;
            defaultApproveNote = "月度承诺审核 ";
        } else if (BusiEnumDefine.FLOW_MONTH_APPRAISAL.equals(flowCode)) {
            //月度考核流程
            approvalType = BusiEnumDefine.FLOW_STATUS_MONTH_EXAM;
            defaultApproveNote = "月度考核评价 ";
        } else if (BusiEnumDefine.FLOW_TRANSFER_FILL.equals(flowCode)) {
            //转正计划填写
            approvalType = BusiEnumDefine.FLOW_STATUS_FINAL_CHECK_PLAN;
            defaultApproveNote = "转正计划审核 ";
        } else if (BusiEnumDefine.FLOW_TRANSFER_APPRAISAL.equals(flowCode)) {
            //转正考核
            approvalType = BusiEnumDefine.FLOW_STATUS_FINAL_EXAM;
            defaultApproveNote = "转正考核评价 ";
        }
        TbApprovalRecordDTO dto = new TbApprovalRecordDTO();
        dto.setSubmit(true);
        dto.setApprovalType(approvalType);
        dto.setFlowId(docId);
        //承诺填写/转正计划流程可通过、驳回、转办，月度考核、转正考核流程可提交、转办
        if ("Agree".equals(formSubmit.getButtonType())) {
            dto.setApprovalResult(BusiEnumDefine.APPROVAL_APPROVE);
            if (BusiEnumDefine.FLOW_MONTH_FILL.equals(flowCode)
                    || BusiEnumDefine.FLOW_TRANSFER_FILL.equals(flowCode)) {
                dto.setApprovalNote(data.getString("opinion"));
            } else if (BusiEnumDefine.FLOW_MONTH_APPRAISAL.equals(flowCode)
                    || BusiEnumDefine.FLOW_TRANSFER_APPRAISAL.equals(flowCode)) {
                dto.setApprovalNote(data.getString("approvalNote"));
                dto.setApprovalScore(data.getString("approvalScore"));
                String approvalResult = data.getString("approvalResult");
                if (!StringUtils.isEmpty(approvalResult)) {
                    dto.setApprovalResult("通过".equals(approvalResult) ? BusiEnumDefine.APPROVAL_APPROVE : BusiEnumDefine.APPROVAL_BACKUP);
                }
            }
        } else if ("Reject".equals(formSubmit.getButtonType())) {
            dto.setApprovalResult(BusiEnumDefine.APPROVAL_BACKUP);
            if (BusiEnumDefine.FLOW_MONTH_FILL.equals(flowCode)
                    || BusiEnumDefine.FLOW_TRANSFER_FILL.equals(flowCode)) {
                dto.setApprovalNote(data.getString("opinion"));
                //驳回时删除提交的附件
                QueryWrapper<SdUploadFile> fileWrapper = Wrappers.query();
                fileWrapper.eq("busi_type", BusiEnumDefine.PROMISE_ATTACHMENT);
                fileWrapper.eq("busi_id", docId);
                List<SdUploadFile> sdUploadFiles = sdUploadFileMapper.selectList(fileWrapper);
                sdUploadFiles.forEach(file -> {
                    commService.deleteSfsFile(file.getFileId());
                });
            } else {
                dto.setApprovalNote(data.getString("approvalNote"));
            }
        } else if ("Transfer".equals(formSubmit.getButtonType())) {
            SdAllWorker transferTarget = commService.getWorkerIdByDomain(formSubmit.getTargetReceiver());
            if (transferTarget != null) {
                tbFlowInstDTO.setApprovalUserIdsTransfer(transferTarget.getWorkerId());
                tbFlowInstDTO.setOpinion(formSubmit.getOpinion());
            }
            List<TbFlowInstDTO> instList = new ArrayList<>();
            instList.add(tbFlowInstDTO);
            tbFlowInstService.transferSpecificFlowInst(instList);
        }

        if (!"Transfer".equals(formSubmit.getButtonType())) {
            dto.setWorkerDomain(formSubmit.getEmpNumber());
            approve(dto);
        }
        if(BusiEnumDefine.FLOW_TRANSFER_APPRAISAL.equals(flowCode)){
            if (data.getString("workerType").equals("RD")) {
                saveIttpPostInfo(data);
            }
        }
    }

    public void saveIttpPostInfo(JSONObject data){
        try {
            String workerId = data.getString("nameAndId").split("/")[1];
            log.info("接收到的workerId为{}", workerId);
            int PostNameID = Integer.valueOf(data.getString("PostNameID"));
            int PostTypeID = Integer.valueOf(data.getString("PostTypeID"));
            if (PostTypeID == 0) {return;}
            IttpClientDTO dto = new IttpClientDTO();
            if (PostNameID != 0) {
                Date workTime = data.getDate("workTime");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:ss:mm.SSSZ");
                String time = dateFormat.format(workTime);
                dto.setStartTime(time);
            }
            dto.setCode(workerId);
            dto.setPostTypeID(PostTypeID);
            dto.setPostNameID(PostNameID);
            String result = ittpClient.saveIttpPostInfo(dto);
            log.info("返回的结果为{}", result);
        } catch (Exception e) {

        }
    }

    @Override
    public List<TbApprovalRecordDTO> getApprovalRecordList(String recordId) {
        QueryWrapper<TbApprovalRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbApprovalRecord::getFlowId, recordId);
        List<TbApprovalRecord> records = tbApprovalRecordMapper.selectList(queryWrapper);
        return TbApprovalRecordMapstruct.INSTANCE.entityToDto(records);
    }

    @Override
    public List<TbApprovalRecordVO> queryNewStaffRecord() {
        List<TbApprovalRecord> recordList = list(new QueryWrapper<TbApprovalRecord>().lambda()
                .eq(TbApprovalRecord::getFlowId, CommonUtil.getActivUser().getUserId())
                .in(TbApprovalRecord::getApprovalType, Arrays.asList(TbApprovalRecord.RESERVE_STAFF_SUBMIT,
                        TbApprovalRecord.RESERVE_STAFF_MODIFIED, BusiEnumDefine.APPROVAL_TYPE_BEISEN_SYNC,
                        BusiEnumDefine.FLOW_STATUS_OFFER_WORKER, BusiEnumDefine.FLOW_STATUS_APPROVAL_PLAN_DATE))
                .orderByDesc(TbApprovalRecord::getApprovalTime)
        );
        List<TbApprovalRecordVO> recordVOS = TbApprovalRecordMapstruct.INSTANCE.entityListToVoList(recordList);
        recordList.forEach(s -> s.setReadFlag(true));
        updateBatchById(recordList);
        return recordVOS;
    }

    @Override
    public Integer countNewStaffNoRead() {
        return count(new QueryWrapper<TbApprovalRecord>().lambda()
                .eq(TbApprovalRecord::getFlowId, CommonUtil.getActivUser().getUserId())
                .eq(TbApprovalRecord::getReadFlag, false)
                .in(TbApprovalRecord::getApprovalType, Arrays.asList(TbApprovalRecord.RESERVE_STAFF_SUBMIT, TbApprovalRecord.RESERVE_STAFF_MODIFIED))
        );
    }

    @Override
    public Integer selectRecordByDate(String id, int type, Date beginDate, Date endDate) {
        return count(new QueryWrapper<TbApprovalRecord>().lambda()
                .eq(TbApprovalRecord::getFlowId, id).eq(TbApprovalRecord::getApprovalType, type)
                .eq(TbApprovalRecord::getFlowArriveTime, beginDate).eq(TbApprovalRecord::getApprovalTime, endDate));
    }
}
